<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>项目阶段API测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .test-section h3 {
            margin-top: 0;
            color: #333;
        }
        button {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 10px 20px;
            margin: 5px;
            border-radius: 3px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 10px;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 3px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
        }
    </style>
</head>
<body>
    <h1>项目阶段API测试</h1>
    
    <div class="test-section">
        <h3>1. 获取项目阶段列表</h3>
        <button onclick="testGetList()">获取列表</button>
        <div id="listResult" class="result"></div>
    </div>

    <div class="test-section">
        <h3>2. 创建项目阶段</h3>
        <button onclick="testCreate()">创建测试阶段</button>
        <div id="createResult" class="result"></div>
    </div>

    <div class="test-section">
        <h3>3. 更新项目阶段</h3>
        <button onclick="testUpdate()">更新阶段</button>
        <div id="updateResult" class="result"></div>
    </div>

    <div class="test-section">
        <h3>4. 删除项目阶段</h3>
        <button onclick="testDelete()">删除阶段</button>
        <div id="deleteResult" class="result"></div>
    </div>

    <div class="test-section">
        <h3>5. 批量删除</h3>
        <button onclick="testBatchDelete()">批量删除</button>
        <div id="batchDeleteResult" class="result"></div>
    </div>

    <div class="test-section">
        <h3>6. 获取统计信息</h3>
        <button onclick="testStats()">获取统计</button>
        <div id="statsResult" class="result"></div>
    </div>

    <script>
        const BASE_URL = 'http://localhost:3000/projectPhase';

        function showResult(elementId, data, isError = false) {
            const element = document.getElementById(elementId);
            element.textContent = JSON.stringify(data, null, 2);
            element.className = `result ${isError ? 'error' : 'success'}`;
        }

        async function testGetList() {
            try {
                const response = await fetch(`${BASE_URL}/list?page=1&limit=10`);
                const data = await response.json();
                showResult('listResult', data);
            } catch (error) {
                showResult('listResult', { error: error.message }, true);
            }
        }

        async function testCreate() {
            try {
                const testData = {
                    status: '未开始',
                    statusType: 'gray',
                    progress: '0%',
                    phase: '测试阶段',
                    phaseType: '自研',
                    leader: '测试负责人',
                    startTime: new Date('2024-02-01'),
                    endTime: new Date('2024-02-28'),
                    period: 28,
                    hour: 224,
                    bonusType: '固定金额',
                    bonusAmount: 3000,
                    calculateWorkTime: true,
                    isConfirmPhase: false,
                    isPaymentPhase: false,
                    deliverFiles: ['测试文档'],
                    files: '测试文档',
                    remark: '这是一个测试阶段',
                    creator: '测试用户'
                };

                const response = await fetch(`${BASE_URL}/create`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(testData)
                });
                const data = await response.json();
                showResult('createResult', data);
            } catch (error) {
                showResult('createResult', { error: error.message }, true);
            }
        }

        async function testUpdate() {
            try {
                const updateData = {
                    status: '进行中',
                    statusType: 'orange',
                    progress: '50%',
                    remark: '阶段已更新'
                };

                const response = await fetch(`${BASE_URL}/update/1`, {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(updateData)
                });
                const data = await response.json();
                showResult('updateResult', data);
            } catch (error) {
                showResult('updateResult', { error: error.message }, true);
            }
        }

        async function testDelete() {
            try {
                const response = await fetch(`${BASE_URL}/delete/5`, {
                    method: 'DELETE'
                });
                const data = await response.json();
                showResult('deleteResult', data);
            } catch (error) {
                showResult('deleteResult', { error: error.message }, true);
            }
        }

        async function testBatchDelete() {
            try {
                const response = await fetch(`${BASE_URL}/batch-delete`, {
                    method: 'DELETE',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ ids: [6, 7, 8] })
                });
                const data = await response.json();
                showResult('batchDeleteResult', data);
            } catch (error) {
                showResult('batchDeleteResult', { error: error.message }, true);
            }
        }

        async function testStats() {
            try {
                const response = await fetch(`${BASE_URL}/stats`);
                const data = await response.json();
                showResult('statsResult', data);
            } catch (error) {
                showResult('statsResult', { error: error.message }, true);
            }
        }

        // 页面加载时自动测试获取列表
        window.onload = function() {
            testGetList();
        };
    </script>
</body>
</html> 